
const app = getApp()
export default {
  props: {
    evaluateType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      show: false,
      focus: false, // 焦点
      keyboardHeight: 0,
      evaluateContent: '', // 评论内容
      evaluatePic: '',  // 评论上传的图片
    }
  },
  watch: {
    // 焦点监听
    focus(vals) {
      vals && uni.onKeyboardHeightChange(res => {
        if (res.height) {
          this.keyboardHeight = res.height;
        }
      })
    }
  },
  computed: {
    //单位是rpx 所以*2
    safeAreaBottom() {
      const { bottom } = app.globalData.systemInfo.safeAreaInsets;
      return bottom
    },
    // 系统名称 android 或者ios
    systemName() {
      const { osName } = app.globalData.systemInfo;
      return osName;
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 打开或者关闭弹窗
     * @param {是否开启} isShow 
     */
    togglePopup(isShow) {
      this.$refs.popup.togglePopup(isShow);
    },
    onChange(e) {
      setTimeout(() => {
        this.$nextTick(() => {
          this.focus = e.show;
          if (!e.show) {
            this.keyboardHeight = 0;
            this.evaluateContent = '';
            this.evaluatePic = '';
            uni.offKeyboardHeightChange();
          }
        })
      }, 200);
    },
    // 选择文件
    handleMedia(e) {
      uni.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album'],
        success: async (res) => {
          const { tempFilePath } = res.tempFiles[0];
          const { suc, result } = await this.$upload({ filePath: tempFilePath });
          if (suc) {
            this.evaluatePic = result;
          }
        },
        fail: err => {
          console.log('err===>', err);
        }
      })
    },
    // 焦点事件
    handleFocus(e) {
      this.keyboardHeight = e.detail.height;
    },
    // 失焦事件
    handleLoseFocus() {
      uni.offKeyboardHeightChange();
      this.keyboardHeight = 0;
    },
    // 删除上传的图片
    handleDelete() {
      this.evaluatePic = '';
    },
    // 发送评论
    handleSend() {
      if (!this.evaluateContent.trim() && !this.evaluatePic) {
        return uni.showToast({title: '请输入评论内容或上传图片', icon: 'none'})
      }
      this.$emit('onConfirm', { 
        content: this.evaluateContent, 
        pic: this.evaluatePic,
        type: this.evaluateType
      })
    }
  }
}